﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

public partial class FinanceUsers_InsertProduct : System.Web.UI.Page
{
    static int DeviceId;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(@"~/FinanceUsers/Home.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        if (Session["EditDivice"] == null)
        {
            Session["EditDivice"] = "False";
        }
        if (Session["EditDivice"].ToString() == "True")
            {
                SubSonic.Query qr = TblDevice.CreateQuery();
                qr.QueryType = SubSonic.QueryType.Update;
                qr.AddUpdateSetting(TblDevice.Columns.Name, txtName.Text);
                qr.AddUpdateSetting(TblDevice.Columns.Model, txtModel.Text);
                qr.WHERE(TblDevice.Columns.Id, SubSonic.Comparison.Equals, DeviceId);
                qr.Execute();

                lblMessage.Text = "وایریش کالا با موفقیت ثبت شد";
                txtName.Text = "";
                txtModel.Text = "";
                GrwDevice.DataBind();
                Session["EditDivice"] = "False";
            }
        
          else
         {
            try
            {

            TblDevice Dev = new TblDevice();

            Dev.Name = txtName.Text;
            Dev.Model = txtModel.Text;
            Dev.Save();



            lblMessage.Text = "کالا با موفقیت ثبت شد";
            txtName.Text = "";
            txtModel.Text = "";
            GrwDevice.DataBind();


            }
            catch {
            lblMessage.Text = "ثبت کالا با خطا مواجه شد";
            }
        }
    }
    protected void GrwDevice_RowCommand(object sender, GridViewCommandEventArgs e)
    {





        if (e.CommandName == "Edit")
        {
            DeviceId = Convert.ToInt32(e.CommandArgument.ToString());

            TblDevice Dev = TblDevice.FetchByID(DeviceId);
            txtName.Text = Dev.Name;
            txtModel.Text = Dev.Model;
            Session["EditDivice"] = "True";

        }
    }
}